import myoa.Employee
import myoa.Role
import myoa.User
import myoa.Company
import myoa.domain.RoleName
import myoa.domain.UserStatus
import org.apache.shiro.crypto.hash.Sha256Hash

class BootStrap {

    def init = { servletContext ->
        if (User.count() == 0) {
            final String ADMIN_NAME = 'root'
            final String ADMIN_PASSWORD = "aaaa"
            User u = new User(name: ADMIN_NAME, password: ADMIN_PASSWORD, lastLogin: new Date())
            u.status = UserStatus.Enabled
            u.addToPermissions("*:*")
            u.encryptPassword()
            u.save()
            assert u.id
            log.info('please login by initial admin account')
        } else {
            log.info('please login by your account')
        }

        if (Role.count() == 0) {
            Role every = new Role(name: RoleName.Every.name())
            every.comment = 'every login user'
            every.addToPermissions('home:index,help')
            every.addToPermissions('user:showPart,show,changePassword,changePasswordDo')
            every.save()

            Role crm = new Role(name: RoleName.CRM.name())
            crm.comment = 'can manage customer info'
            crm.addToPermissions('company:*')
            crm.addToPermissions('employee:*')
            crm.save()

            Role project = new Role(name: RoleName.PM.name())
            project.comment = 'project manager'
            project.addToPermissions('project:*')
            project.addToPermissions('proUser:*')
            project.addToPermissions('proCompany:*')
            project.addToPermissions('proEmployee:*')
            project.addToPermissions('task:*')
            project.save()

            Role emp = new Role(name: RoleName.EMP.name())
            emp.comment = 'employee can create/edit whose own expense'
            emp.addToPermissions('expenseDetail:*')
            emp.addToPermissions('expenseGroup:list,create,save,upload,uploadDo,show,edit,update,delete')
            emp.save()

            Role manager = new Role(name: RoleName.MGR.name())
            manager.comment = 'manager like Wilson'
            manager.addToPermissions('report:search')
            manager.addToPermissions('expenseGroup:approve')
            manager.save()

            Role accounting = new Role(name: RoleName.ACC.name())
            accounting.comment = 'accounting like Guanjing'
            accounting.addToPermissions('expenseGroup:writeoff')
            accounting.save()

            Role system = new Role(name: RoleName.SYS.name())
            system.comment = 'system admin like Calvin'
            system.addToPermissions('fileStore:*')
            system.addToPermissions('sample:*')
            system.addToPermissions('user:*')
            system.addToPermissions('role:*')
            system.save()
        }


    }
    def destroy = {
    }
}
